package sk.tuke.constraintbuilder.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

import sk.tuke.constraintbuilder.analyzer.RulesetManager;
import sk.tuke.constraintbuilder.views.components.ApplyRulesetDialog;

public class ApplyRuleset extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		Shell shell = HandlerUtil.getActiveShell(event);
	    ISelection sel = HandlerUtil.getActiveMenuSelection(event);
	    IStructuredSelection selection = (IStructuredSelection) sel;

	    Object firstElement = selection.getFirstElement();
	    if (firstElement instanceof IJavaProject) {
			ApplyRulesetDialog dialog = new ApplyRulesetDialog(shell);
			dialog.create();
			if (dialog.open() == Window.OK) {
				RulesetManager.getInstance().applyRulesetsOnProject((IJavaProject) firstElement, dialog.getAppliedRulesets());
			} 
	    } else {
	      MessageDialog.openInformation(shell, "Info","Please select a Java project");
	    }

		return null;
	}

}
